/* Copyright 2012 Ilkka Kiistala

This file is part of the Crossmodal-Framework. The Crossmodal-Framework is free software: you can redistribute it and/or modify it under the terms of the GNU 
General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Crossmodal-Framework is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along 
with the Crossmodal Framework.  If not, see <http://www.gnu.org/licenses/> */

package com.example.notificationtestapp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class NotificationTestApp extends Activity {

	InputStream is;
	String url_json = "http://kiistala.com/cm/json";

	protected void dummy(Bundle icicle) {

		super.onCreate(icicle);

		setContentView(R.layout.main);
		Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
		ByteArrayOutputStream bao = new ByteArrayOutputStream();
		bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
		byte [] ba = bao.toByteArray();
		String ba1=Base64.encodeBytes(ba);
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("image",ba1));

		try{
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://kiistala.com/cm/base64up.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();

		}catch(Exception e){
			Log.e("log_tag", "Error in http connection "+e.toString());
		}

	}

	@Override
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
		String result = null;

    	switch (item.getItemId()) {
        case R.id.about:
            // Logger.d("Displaying the about box");
            displayAboutBox();
            return true;
        //case R.id.load_experiments:
    		//String result = null;

            // Logger.d("Displaying the team configuration");
            // displayConfiguration();
    		
    		/*
    		try {
    			result = HTTPGetCall(url_json);
    		} catch (MalformedURLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		*/
    		
    		// import JSON

 		
    		InputStream inputStream = null;
    		//inputStream;
    		// json is UTF-8 by default i beleive
    		BufferedReader reader = null;
    		try {
    			reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		StringBuilder sb = new StringBuilder();
    		String line = null;  

    		try {
				while ((line = reader.readLine()) != null)
				{
					sb.append(line + "\n");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		result = sb.toString();
    		
    		// Context context = getApplicationContext();
    		int duration = Toast.LENGTH_LONG;
    		Toast t;
    		String text = "Result: " + result;
    		t = Toast.makeText(context, text, duration);
    		t.show();

    		
            // return true;
    		return true; 
            
        default:

        	String text2 = "Result: " + result;
    		t = Toast.makeText(context, text2, duration);
    		t.show();

        	
            //Logger.e("Unknown menu item selected");
            return false;
        }
    }

    protected void displayAboutBox() {
        startActivity(new Intent(this, About.class));
    }

	protected String HTTPGetCall(String WebMethodURL) throws IOException, MalformedURLException 
	{ 
		StringBuilder response = new StringBuilder(); 

		//Prepare the URL and the connection 
		URL u = new URL(WebMethodURL); 
		HttpURLConnection conn = (HttpURLConnection) u.openConnection(); 

		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) 
		{ 
			//Get the Stream reader ready 
			BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()),8192); 

			//Loop through the return data and copy it over to the response object to be processed 
			String line = null; 

			while ((line = input.readLine()) != null) 
			{ 
				response.append(line); 
			} 

			input.close(); 
		} 

		return response.toString(); 
	}

}
